Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

oniyi-http-client

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

oniyi-http-client

Adding a plugin interface to "request" that allows modifications of request parameters and response data

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

oniyi-http-client

NPM version Dependency Status

Adding a plugin interface to request that allows modifications of request parameters and response data

Installation

$ npm install --save oniyi-http-client

Usage

Note: this module does not support streams, yet

var OniyiHttpClient = require('oniyi-http-client');

var client = new OniyiHttpClient({
  defaults: {
    headers: {
      'Accept-Language': 'en-US,en;q=0.8',
      Host: 'httpbin.org',
      'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
      Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    }
  }
});

client.makeRequest('http://httpbin.org/headers', {
  method: 'GET'
  // jar: client.jar(new redisStore(redisClient))
}, function (err, response, body) {
  if (err) {
    logger.warn('got an error');
    if (err.stack) {
      logger.error(err.stack);
    } else {
      logger.error(err);
    }
    process.exit(0);
  }
  if (response) {
    logger.debug('statusCode: %d', response.statusCode);
    logger.debug('headers: ', response.headers);
    logger.debug('body: ', body);
  }
  process.exit(0);
});

Using plugins

This creates a plugin named plugin-2 which adds a request-header with name and value plugin-2. Also, it stores some data in a local variable and overrides the original callback function to print that stored data on response. Afterwards it calls the original callback function.

var plugin2 = {
  name: 'plugin-2',
  load: function (req, params, callback) {
    var plugin2Storage = {};
    setTimeout(function () {
      params.headers = params.headers || {};
      params.headers['plugin-2'] = 'plugin-2';

      var name = 'Bam Bam!';

      var originalCallback = params.callback;

      params.callback = function (err, response, body) {
        logger.info('Name in this plugin\'s store: %s', name);
        return originalCallback(err, response, body);
      };

      callback(null, params);
    }, 500);
  }
};

client
  .use(plugin2)
  .makeRequest('http://httpbin.org/headers', {
    method: 'GET'
    // jar: client.jar(new redisStore(redisClient))
  }, function (err, response, body) {
    if (err) {
      logger.warn('got an error');
      if (err.stack) {
        logger.error(err.stack);
      } else {
        logger.error(err);
      }
      process.exit(0);
    }
    if (response) {
      logger.debug('statusCode: %d', response.statusCode);
      logger.debug('headers: ', response.headers);
      logger.debug('body: ', body);
    }
    process.exit(0);
  });

License

MIT © Benjamin Kroeger

Keywords

FAQs

Package last updated on 09 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc